package com.dudu.controller;

import java.util.Calendar;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAccountManageService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.ILogService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AccountManageServiceImpl;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.LogService;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.MD5Utils;

import core.CoreService;

/**
 * 账号扩展信息管理
 * 
 * @author gaoxs t_account_extend
 * 
 *         accountExtend
 */
public class AccountController extends CoreService implements IAccountController {
    // 当前登陆用户
    private final String KEY_CURRENT_USER = "key_current_user";
    // 用于记录相应接口调用的日志
    private ILogService logService = new LogService();
    // 账号相关
    private IAccountManageService iAccountService = new AccountManageServiceImpl();
    // 账号
    private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
    // 应用扩展信息相关
    private IAppExtendService appExtendService = new AppExtendServiceImpl();

    /**
     * 查询所有组织的account信息
     * 
     * @return
     */
    public String queryAll() {
	log.info("AccountController.queryAll");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 最后返回的对象
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	// 判断如果用户信息不为空则继续下一步
	if (currentUser != null) {
	    // 获取页面分页用信息
	    int pageSize = Integer.valueOf(this.getRequest().getParameter("size"));
	    int start = Integer.valueOf(this.getRequest().getParameter("start"));
	    int page_no = start / pageSize + 1;
	    Map<String, String> map = this.convertParam();
	    String isv_name = map.get("isv_name");
	    String app_name = map.get("app_name");
	    String cust_account = map.get("cust_account");
	    JSONObject dataobject = iAccountService.queryAll(page_no, pageSize, isv_name, app_name, cust_account);
	    object.put("data", dataobject);
	    object.put("flag", "000000");
	    object.put("desc", "success");
	} else {
	    object.put("flag", "100000");
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	log.info("调用时间(单位：秒)为：" + during);
	return returnData(object.toString());
    }

    /**
     * 获取扩展信息
     * 
     * @return
     */
    public String getExtendInfo() {

	log.info("AccountController.getExtendInfo");
	// 最后返回的对象
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	// 判断如果用户信息不为空则继续下一步
	if (currentUser != null) {
	    String cust_account = this.getRequest().getParameter("cust_account");
	    AccountBean bean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
	    object.put("flag", "000000");
	    object.put("desc", "success");
	    object.put("bean", bean);
	} else {
	    object.put("flag", "100000");
	}
	return returnData(object.toString());
    }

    /**
     * 修改或者新增应用扩展信息
     * 
     * @return
     */
    public String doExtendInfo() {
	log.info("AppController.doExtendInfo");
	// 最后返回的对象
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	// 判断如果用户信息不为空则继续下一步
	if (currentUser != null) {
	    // 获取前台传入的data数据
	    String dataStr = (String) this.getRequest().getParameter("data");
	    // 转化为json对象
	    JSONObject data = JSONObject.fromObject(dataStr);
	    String cust_account = data.getString("cust_account");
	    String key_private = data.getString("key_private");
	    if (EnumUtility.isBlank(key_private)) {
		key_private = MD5Utils.getMd5Value();
	    }
	    String key_public = data.getString("key_public");
	    if (EnumUtility.isBlank(key_public)) {
		key_public = MD5Utils.getMd5Value();
	    }
	    String linker_name = data.getString("linker_name");
	    String linker_phone = data.getString("linker_phone");
	    String linker_email = data.getString("linker_email");
	    boolean isHasExtend = data.getBoolean("isHasExtend");
	    String app_key = data.getString("app_key");
	    AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
	    String url_callstatus = data.getString("url_callstatus");
	    // 判断如果url_callstatus是否为空
	    if (StringUtils.isBlank(url_callstatus) && EnumUtility.isNotBlank(appExtendBean)) {
		// 如果传入为空，则使用app配置
		url_callstatus = appExtendBean.getUrl_callstatus();
	    }
	    String url_ticket = data.getString("url_ticket");
	    // 判断如果url_ticket是否为空
	    if (StringUtils.isBlank(url_ticket) && EnumUtility.isNotBlank(appExtendBean)) {
		// 如果传入为空，则使用app配置
		url_ticket = appExtendBean.getUrl_ticket();
	    }
	    String url_authentication = data.getString("url_authentication");
	    // 判断如果url_authentication是否为空
	    if (StringUtils.isBlank(url_authentication) && EnumUtility.isNotBlank(appExtendBean)) {
		// 如果传入为空，则使用app配置
		url_authentication = appExtendBean.getUrl_authentication();
	    }
	    String url_ivrdigit = data.getString("url_ivrdigit");
	    // 判断如果url_ivrdigit是否为空
	    if (StringUtils.isBlank(url_ivrdigit) && EnumUtility.isNotBlank(appExtendBean)) {
		// 如果传入为空，则使用app配置
		url_ivrdigit = appExtendBean.getUrl_ivrdigit();
	    }
	    String url_record = data.getString("url_record");
	    String url_sms = data.getString("url_sms");
	    String url_infocontrol = data.getString("url_infocontrol");
	    String billing_platform = data.getString("billing_platform");
	    // 判断如果billing_platform是否为空
	    if (StringUtils.isBlank(billing_platform) && EnumUtility.isNotBlank(appExtendBean)) {
		// 如果传入为空，则使用app配置
		url_ivrdigit = appExtendBean.getBilling_platform();
	    }
	    AccountBean bean = new AccountBean();
	    if (isHasExtend) {
		// 已配置update
		bean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
	    }
	    bean.setCust_account(EnumUtility.trimNumm(cust_account));
	    bean.setKey_public(EnumUtility.trimNumm(key_public));
	    bean.setKey_private(EnumUtility.trimNumm(key_private));
	    bean.setLinker_name(EnumUtility.trimNumm(linker_name));
	    bean.setLinker_phone(EnumUtility.trimNumm(linker_phone));
	    bean.setLinker_email(EnumUtility.trimNumm(linker_email));
	    bean.setUrl_ticket(EnumUtility.trimNumm(url_ticket));
	    bean.setUrl_callstatus(EnumUtility.trimNumm(url_callstatus));
	    bean.setUrl_authentication(EnumUtility.trimNumm(url_authentication));
	    bean.setUrl_ivrdigit(EnumUtility.trimNumm(url_ivrdigit));
	    bean.setUrl_record(EnumUtility.trimNumm(url_record));
	    bean.setBilling_platform(EnumUtility.trimNumm(billing_platform));
	    bean.setUrl_sms(url_sms);
	    bean.setUrl_infocontrol(url_infocontrol);

	    String appin = "-1";
	    if (isHasExtend) {
		// 已配置
		appin = accountExtendService.updateAccountExtend(bean) + "";
		// 日志输出信息
		logService.log(OperateTypeEnum.EDIT.getKey(), currentUser.getId(), currentUser.getName(), "t_account_extend", "对账号标识为:" + bean.getCust_account() + "进行修改应用扩展信息url_ticket:"+url_ticket+"url_callstatus:"+url_callstatus+"url_ivrdigit:"+url_ivrdigit);
	    } else {
		bean.setCreate_user(currentUser.getName());
		bean.setAccount_identify("");
		// 未配置
		appin = accountExtendService.insertAccountExtend(bean).getString("result");
		// 日志输出信息
		logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), "t_account_extend", "对账号标识为:" + bean.getCust_account() + "进行新增应用扩展信息url_ticket:"+url_ticket+"url_callstatus:"+url_callstatus+"url_ivrdigit:"+url_ivrdigit);
	    }
	    if (!appin.equals("-1")) {
		object.put("flag", "000000");
		object.put("desc", "success");
	    } else {
		object.put("flag", "100000");
	    }

	} else {
	    object.put("flag", "100000");
	}
	return returnData(object.toString());
    }

}
